package DML;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import util.Methods;
import DuiXiang.TableModel;

public class util {
	public static List<List<String>> tableContent;

	public static List<TableModel> ReadTableModelList(String dbfpath) {
		List<TableModel> result = new ArrayList<TableModel>();
		File file = new File(dbfpath);
		if (!file.exists())
			return null;
		FileReader fileReader = null;
		BufferedReader bufferReader = null;
		try {
			fileReader = new FileReader(file);
			bufferReader = new BufferedReader(fileReader);
			String line;
			TableModel tm;
			try {
				while ((line = bufferReader.readLine()) != null) {
					tm = new TableModel();
					String[] tableModelDetails = line.split(",");
					tm.setsFieldName(tableModelDetails[0]);
					tm.setsType(tableModelDetails[1]);
					tm.setiSize(Integer.parseInt(tableModelDetails[2]));
					tm.setbKey(tableModelDetails[3].charAt(0));
					tm.setbNullFlag(tableModelDetails[4].charAt(0));
					tm.setbValidFlag(tableModelDetails[5].charAt(0));
					result.add(tm);
				}
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			try {
				bufferReader.close();
				fileReader.close();	
				file=null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public static List<List<String>> readTable(String datpath) {
			File file = new File(datpath);
			if (!file.exists())
				return null;
			FileReader fileReader = null;
			BufferedReader bufferReader = null;
			try {
				fileReader = new FileReader(file);
				bufferReader = new BufferedReader(fileReader);
				String line;
				tableContent = new ArrayList<List<String>>();
				try {
					while ((line = bufferReader.readLine()) != null) {
						String[] elementsOfALine = line.split(",");
						List<String> aLine = new ArrayList<String>(
								elementsOfALine.length);
						for (String str : elementsOfALine)
							aLine.add(str);
						tableContent.add(aLine);
					}
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} finally {
				try {
					bufferReader.close();
					fileReader.close();	
					file=null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		return tableContent;
	}

	public static boolean IfPrimarykeyExist(String[] values,
			List<TableModel> tableModelList, String datpath) {
		if (tableContent == null)
			readTable(datpath);
		int keyIndex = 0;
		for (int i = 0; i < tableModelList.size(); i++) {
			if (tableModelList.get(i).getbKey() == 'y') {
				keyIndex = i;
				break;
			}
		}
		for (List<String> aline : tableContent) {
			if (aline.get(keyIndex).equals(values[keyIndex]))
				return true;
		}
		return false;
	}
}
